package tp3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sun.net.www.URLConnection;

/**
 * Servlet implementation class pruebaApp
 */
@WebServlet("/pruebaApp")
public class pruebaApp extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public pruebaApp() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("se invoco el servlet");
		
		// response.getOutputStream().write("HTML DE REGRESO".getBytes());
		
		
		//primeras 3 lineas logoro coneccion con la appserver
		URL myURL = new URL("http://localhost:8080/lab3-AppServer/prueba");
		URLConnection myURLConnection = (URLConnection) myURL.openConnection();
		myURLConnection.connect();
		BufferedReader reader = new BufferedReader (new InputStreamReader(myURLConnection.getInputStream()));
		StringBuffer sb = new StringBuffer();
		String linea = "";
		while ((linea = reader.readLine()) != null) 
		{
			sb.append(linea);
		}
		response.getOutputStream().write(sb.toString().getBytes());
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
